package com.cmict.data.infra.dal.mysql.message;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.framework.mybatis.core.mapper.BaseMapperX;
import com.cmict.data.infra.api.emus.MessageTypeEnum;
import com.cmict.data.infra.controller.admin.message.vo.AnnouncementQO;
import com.cmict.data.infra.dal.dataobject.message.Announcement;
import com.cmict.data.infra.enums.message.AnnouncementStatusEnum;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 公告信息
 *
 * @author hccake 2020-12-15 17:01:15
 */
@Mapper
public interface AnnouncementMapper extends BaseMapperX<Announcement> {

    /**
     * 分页查询
     *
     * @param qo 查询对象
     * @return 分页结果数据 PageResult
     */
    default PageResult<Announcement> queryPage(AnnouncementQO qo) {
        QueryWrapper<Announcement> queryWrapper = new QueryWrapper<>();
        if (qo.getValue() != null) {
            queryWrapper.lambda().and(qw -> qw.like(Announcement::getTitle, qo.getValue())
                    .or().like(Announcement::getContent, qo.getValue()));
        }
        queryWrapper.lambda().ne(Announcement::getType, MessageTypeEnum.SYSTEM_NOTICE.getValue())
                .in(qo.getStatusList() != null && qo.getStatusList().size()>0,Announcement::getStatus, qo.getStatusList())
                .in(qo.getTypes()!=null && qo.getTypes().size()>0,Announcement::getType,qo.getTypes())
                .eq(Announcement::getDeleted, false)
                .orderBy(true ,qo.getSortord()!=null && qo.getSortord().equals("ASC"),Announcement::getCreateTime);
//        .orderBy(qo.getSortord() != null ,qo.getSortord().equals("ASC"),Announcement::getCreateTime);


        return selectPage(qo, queryWrapper);
    }


    /**
     * 分页查询
     *
     * @param qo 查询对象
     * @return 分页结果数据 PageResult
     */
    default PageResult<Announcement> queryPageForUser(AnnouncementQO qo) {
        QueryWrapper<Announcement> queryWrapper = new QueryWrapper<>();
        if (qo.getValue() != null) {
            queryWrapper.lambda().and(qw -> qw.like(Announcement::getTitle, qo.getValue())
                    .or().like(Announcement::getContent, qo.getValue()));
        }
        if (CollectionUtils.isNotEmpty(qo.getAnnouncementIds())) {
            queryWrapper.lambda().in(Announcement::getId, qo.getAnnouncementIds());
        }else {
            return new PageResult<>();
        }
        queryWrapper.lambda()
                .in( qo.getTypes()!=null && qo.getTypes().size()>0, Announcement::getType,qo.getTypes())
                .eq(Announcement::getDeleted, false)
                .orderBy(true ,qo.getSortord()!=null && qo.getSortord().equals("ASC"),Announcement::getCreateTime);
//                .orderBy(true ,true,Announcement::getCreateTime);
        return selectPage(qo, queryWrapper);
    }

    default PageResult<Announcement> queryRemindPageForUser(AnnouncementQO qo) {
        QueryWrapper<Announcement> queryWrapper = new QueryWrapper<>();
//        if (qo.getValue() != null) {
//            queryWrapper.lambda().and(qw -> qw.like(Announcement::getTitle, qo.getValue())
//                    .or().like(Announcement::getContent, qo.getValue()));
//        }
        if (CollectionUtils.isNotEmpty(qo.getAnnouncementIds())) {
            queryWrapper.lambda().in(Announcement::getId, qo.getAnnouncementIds());
        }else{
            queryWrapper.lambda().in(Announcement::getId, 0);
        }
        queryWrapper.lambda().in(Announcement::getType, qo.getTypes());
        queryWrapper.lambda().eq(Announcement::getStatus, qo.getStatus());
        queryWrapper.lambda().eq(Announcement::getDeleted, false).orderByDesc(Announcement::getId);

        return selectPage(qo, queryWrapper);
    }


    /**
     * 更新公共（限制只能更新未发布的公共）
     *
     * @param announcement 公共信息
     * @return 更新是否成功
     */
    default boolean updateIfUnpublished(Announcement announcement) {
        int flag = this.update(announcement,
                Wrappers.<Announcement>lambdaUpdate().eq(Announcement::getId, announcement.getId())
                        .ne(Announcement::getStatus, AnnouncementStatusEnum.PUBLISHED.getValue()));
        return SqlHelper.retBool(flag);
    }

    /**
     * 根据参数获取当前用户拉取过，或者未拉取过的有效的公告信息
     *
     * @param userId 用户ID
     * @return 公告信息列表
     */
    List<Announcement> listUserAnnouncements(@Param("userId") Long userId);

    @Select("${sql}")
    LocalDateTime selectMaxTime(String s);

    @Select("SELECT LAST_INSERT_ID();")
    Long getLAST_INSERT_ID();
}
